package com.client.dto;

import net.sf.json.JSONObject;

/**用于序列化或实例化请求或回复，这里称为信封
 *
 */
public class Envelope {
	private int code;	//信封状态
	private String message;	//信封信息
	private Object object;	//信封携带对象

	/**根据状态、信息、携带对象来实例化一个信封
	 *
	 * @param code
	 * @param message
	 * @param obj
	 */
	public Envelope(int code,String message,Object obj) {
		this.code=code;
		this.message=message;
		this.object=obj;
	}

	/**根据信封序列化后的json来实例化一个信封
	 *
	 * @param json
	 */
	public Envelope(String json){
		JSONObject env=JSONObject.fromObject(json);
		code=env.getInt("code");
		message=env.getString("message");
		object=env.get("object");
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Object getObject() {
		return object;
	}
	public void setObject(Object object) {
		this.object = object;
	}

	/**将信封序列化
	 *
	 * @return
	 */
	public String toString(){
		JSONObject json=new JSONObject();
		json.put("code", code);
		json.put("message", message);
		if(object!=null)
			json.put("object", object.toString());
		return json.toString();

	}

}
